package org.alesv.entranceExams.web.controller;

import javax.servlet.http.HttpSession;

import org.alesv.common.exceptions.ServiceException;
import org.alesv.common.logging.annotations.LogDebug;
import org.alesv.entranceExams.service.WordsDictionaryService;
import org.alesv.entranceExams.web.UrlConstants;
import org.alesv.entranceExams.web.form.NewCharForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Main controller to manage the game.
 */
@Controller
public class LetsPlayController extends GameOfGhostBaseController {

	/**
	 * Usado para escribir trazas.
	 */
	private final Logger log = LoggerFactory
			.getLogger(LetsPlayController.class);

	@Autowired
	private WordsDictionaryService wordsDictionaryService;

	/**
	 * Method executed befor Lets play view.
	 * 
	 * @return String
	 */
	@RequestMapping(UrlConstants.URL_LETSPLAY)
	@LogDebug(loggerClass = LetsPlayController.class)
	public String letsPlay(Model model, NewCharForm form, HttpSession session) {
		String resultView = UrlConstants.VIEW_LETSPLAY;

		if (form.getNewChar() == null) {
			form.setNewChar("");
		}
		if (form.getFullWord() == null) {
			form.setFullWord("");
		}

		log.debug("New char to add: " + form.getNewChar());

		if (form.getNewChar() != null && form.getNewChar().length() > 1) {
			model.addAttribute(UrlConstants.PARAM_ERROR_MESSAGE,
					"letsPlay.error.onlyOneCharAllowed");
			log.info("The user has try to send more than one character: "
					+ form.getNewChar());
		} else {
			String part = form.getFullWord() + form.getNewChar();

			if (part != null && !part.equals("")
					&& wordsDictionaryService.existWord(part)) {
				model.addAttribute(UrlConstants.PARAM_ERROR_MESSAGE,
						"letsPlay.error.existingWordYouLoose");
				form.setFullWord(part);
				form.setNewChar("");
				return resultView;
			}

			String nextWord = "";

			if (form.getNewChar() != null && !form.getNewChar().equals("")) {
				try {
					nextWord = wordsDictionaryService.getNextWinnerWord(part);
				} catch (ServiceException e) {
					log.debug(
							"Error trying to add the letter '"
									+ form.getNewChar()
									+ "' to the previous text '"
									+ form.getFullWord() + "'.", e);
					model.addAttribute(UrlConstants.PARAM_ERROR_MESSAGE,
							"letsPlay.error.impossibleAddLetter");
					form.setNewChar("");

					return resultView;
				}
				if (nextWord == null || nextWord.equals(part)) {
					model.addAttribute(UrlConstants.PARAM_ERROR_MESSAGE,
							"letsPlay.error.iLoose");
					// nextWord =
					// wordsDictionaryService.getNextLooserWord(part);
					// form.setFullWord(nextWord);
					form.setNewChar("");

					return resultView;
				}
				String previousPartWord = form.getFullWord();
				String newPartWord = nextWord.substring(0,
						previousPartWord.length() + 2);
				nextWord = newPartWord;
			}

			form.setFullWord(nextWord);
			form.setNewChar("");
		}

		return resultView;
	}

}
